{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b4b9d4dd-d6c5-451a-8922-6e1189b4a9ea",
   "metadata": {},
   "source": [
    "# 第三节课读书笔记\n",
    "## python变量的命名"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a73c1fa-1364-44e2-9adf-5f1d92053fe6",
   "metadata": {},
   "source": [
    "Python中变量的命名法： 字母+数字+\"_\"三类组成;首字不能是数字；不能与‘保留字’相同。\n",
    "\n",
    "以后编程的时候命名变量的要求：\n",
    "\n",
    "* 尽可能不要用i,j,k这样无意义的变量名称\n",
    "* 尽可能用英文表示变量的含义\n",
    "*  如果有多个单词组成的变量，请用下划线连接\n",
    "\n",
    "- 程序代码示例 \n",
    " ```python\n",
    "i = 1\n",
    "print(i)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ecd4a67-3a84-440b-b69e-b7383fdde3d3",
   "metadata": {},
   "source": [
    "* Python变量的类型（Variable type）\n",
    "\n",
    "Python 变量的类型分三类\n",
    "\n",
    "* 数值型：包括‌整数型（int）、‌浮点型（float）、‌复数型（complex）和‌布尔型（bool）\n",
    "* 字符型：str\n",
    "* 空型：None\n",
    "\n",
    "class:类\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f3c38fb3-a5ac-42d8-8fdc-f3448ac4803b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "x=1.\n",
    "print(type(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "93a29f9b-50c4-4a6e-9ceb-5ab65d510dce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(x,float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "096378be-3b48-43c0-9186-2f4432c6f1b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(x,int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0b5976b0-5164-4f93-a964-51c97433af5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='1'\n",
    "b=1\n",
    "print(a,b)\n",
    "\n",
    "a==b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cac93f5e-d528-4acf-b1ec-68f09e68ad8a",
   "metadata": {},
   "source": [
    "# Python的运算符\n",
    "\n",
    "-数值计算：+ - * / ** % // += -= *= /= == <> >= <= & | ^ ! () \n",
    "\n",
    "-字符串运算符：+, *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d4bc5157-76bc-48c1-a108-d1ae561ef408",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'GuangzhouUniversity'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'Guangzhou'\n",
    "b = 'University'\n",
    "a+b\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2273af15-2ba1-481d-a37a-0966c07f4453",
   "metadata": {},
   "source": [
    "# Python变量的扩展\n",
    "\n",
    "* bool  ->  int  ->  float  ->  complex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84c33ae0-3928-41f4-98bb-3eab2cceb647",
   "metadata": {},
   "source": [
    "# Python字符串函数与切片\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "db9a1e97-91e8-4810-bb62-9533973e8810",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='GuangZhou University'\n",
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c76c39d0-d0ea-45d6-9884-be8d6d27e1a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Zhou' in a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1bd201c3-ca4b-4745-a4a5-6221b2207439",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zh'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'Guangzhou'\n",
    "a[5:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d1cc44e3-1ed3-46d2-8f7a-0f16e392aef4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3769444444444444\n"
     ]
    }
   ],
   "source": [
    "b = '10\t \t*\t00 22 37.0\t2\t+63 52.0\t2\t110.0\t17.0\t \t5.4\t00 25 25.1\t+64 08 36'\n",
    "deg = b[7:9]\n",
    "minute = b[10:12]\n",
    "sec = b[13:17]\n",
    "print(eval(deg) + eval(minute)/60 + eval(sec)/3600.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "790edefb-d2d0-40a7-b75c-2c177214ccac",
   "metadata": {},
   "source": [
    "# Python的格式化输出\n",
    "格式化输出分别有3种技术\n",
    "\n",
    "* -%\n",
    "* -format\n",
    "* -fString\n",
    "\n",
    "'格式化说明' % ( 要显示输出的变量 ）\n",
    "     %d %f %s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fffcc859-16be-4d34-9ceb-45cd7ed62241",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3   4\n",
      " 100 101\n",
      "003.1416\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = 100\n",
    "pi = 3.14159265358979\n",
    "print('%4d%4d' % (a,b))\n",
    "print('%4d%4d' % (c,c+1))\n",
    "\n",
    "print('%08.4f' % pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78977656-6fcc-4e91-9a4c-e97b37d46bfc",
   "metadata": {},
   "source": [
    "# 1.随机产生100个（0.100）的浮点数；2.输入这100个数字。%8.3f，输出； 3.每一行输出10个； 4.输出10行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "be93eede-6a24-478b-b2e3-7e90e0426728",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  47.367   2.603  90.071  98.389  43.218  98.935  95.898  66.284  45.221  58.878\n",
      "  68.836  95.930  53.945  39.001  12.259  85.649   6.686  20.403  55.997  16.130\n",
      "  41.436  90.276  25.807  42.027  73.744  76.041  46.822   8.381  97.599  91.271\n",
      "  50.660  15.997  40.092  40.334  40.644  67.218   7.244  95.595  52.094  96.554\n",
      "  39.876  87.242  12.376  85.751  28.193  23.909  70.631  49.840  56.427  22.382\n",
      "  15.667  52.849  88.612  78.389  16.432  68.039  67.756  87.433  60.499   8.809\n",
      "  68.046  20.942  91.591  76.561  58.167  76.878  93.568  61.052  43.713  86.372\n",
      "   4.238  29.581  10.259  88.956   3.243  21.974  19.338  21.403   8.638  24.408\n",
      "  36.310   1.973  75.829  54.356  50.341  45.233  85.773  77.028  67.874   2.057\n",
      "  34.379  52.277  82.092  47.821  74.777  87.092  97.490  82.297  98.849  99.376\n"
     ]
    }
   ],
   "source": [
    "import random \n",
    "\n",
    "for i in range(100):\n",
    "    print('%8.3f' % (random.random()*100),end='')\n",
    "    if ( (i+1) % 10 == 0):\n",
    "     print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94f790e3-230c-4faf-9822-ba262bd700c6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
